package main

import (
	"fmt"
	"net"
	"net/http"
	"time"
)

func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
	return func(netw, addr string) (net.Conn, error) {
		conn, err := net.DialTimeout(netw, addr, cTimeout)
		if err != nil {
			return nil, err
		}
		conn.SetDeadline(time.Now().Add(rwTimeout))
		return conn, nil
	}
}

func main() {

	connectTimeout := 5 * time.Second
	readWriteTimeout := 50 * time.Second

	client := &http.Client{
		Transport: &http.Transport{
			Dial: TimeoutDialer(connectTimeout, readWriteTimeout),
		},
	}
	url := "http://ssl_theme.8um0.cn/health"
	request, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		fmt.Println("req error :" + err.Error())
		return
	}
	for {
		response, err := client.Do(request)
		if err != nil {
			fmt.Println("do error,err:" + err.Error())
			return
		}
		if response.StatusCode == 200 {
			//str, _ := ioutil.ReadAll(response.Body)
			//bodyStr := string(str)
			fmt.Println("ok")
		}else{
			// Warning
			fmt.Println(response.Status)
		}
		time.Sleep(1 * time.Second)
	}
}
